(function() {
	var log = panda.log("servlets.page");
	var error = 'Page Servlet Error: "{}", when excuting page.{}() with params: {}';

	var servlet = javax.servlet.http.HttpServlet;
	servlets.page = servlet({ service: service });

	function service(req, res) {
		var result = run(req, res);
		res.contentType = "text/html; charset=UTF8";
		res.writer.write(result);
	}

	function run(req, res) {
		var paths, action, params;

		try {
			paths = req.requestURI.substr(1).split("/");

			if (paths[0]) {
				action = paths[0];
				params = paths.splice(1);
			} else {
				action = "index";
			}

			var proxy = panda.proxy("page");
			return proxy[action](params, req, res);
		} catch (e) {
			params = JSON.stringify(params) + "";
			log.error(error, [e, action + "", params]);
			return e.toString();
		}
	}
}());
